﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WooCoo.ObjectModel.ManagedProperty
{
    /// <summary>
    /// 托管属性的元数据接口
    /// </summary>
    public interface IPropertyInfo
    {
        /// <summary>
        /// 属性名
        /// </summary>
        string Name { get; }
        /// <summary>
        /// 属性类型
        /// </summary>
        Type Type { get; }
        /// <summary>
        /// 属性的默认值
        /// </summary>
        object DefaultValue { get; }
        /// <summary>
        /// 是否只读
        /// </summary>
        bool isReadOnly { get; set; }
        /// <summary>
        /// 属性列表索引
        /// </summary>
        int Index { get; set; }
        /// <summary>
        /// 创建字段信息
        /// </summary>
        /// <returns></returns>
        IFieldData CreateFieldData(string name);
        /// <summary>
        /// 友好名
        /// </summary>
        string FriendlyName { get; }
    }

    /// <summary>
    /// 托管属性元数据的泛型实现
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PropertyInfo<T> : IPropertyInfo
    {        
        public PropertyInfo(string name)
        {
            this.Name = name;
            if (typeof(T).Equals(typeof(string)))
                this.DefaultValue = (T)((object)string.Empty);
            else
                this.DefaultValue = default(T);            
        }

        public PropertyInfo(string name, T defaultValue, string mappingName)
        {
            this.Name = name;
            this.DefaultValue = defaultValue;
            this.FriendlyName = mappingName;
        }

        public string Name { get; private set; }

        public string FriendlyName { get; set; }

        public Type Type
        {
            get { return typeof(T); }
        }

        public T DefaultValue { get; private set; }
       
        object IPropertyInfo.DefaultValue
        {
            get { return this.DefaultValue; }
        }
        /// <summary>
        /// 是否只读
        /// </summary>
        public bool isReadOnly { get; set; }
        /// <summary>
        /// 在属性缓存中的索引
        /// </summary>
        public int Index { get; set; }
        
        public IFieldData CreateFieldData(string name)
        {
            return new FieldData<T>();
        }
    }
}
